from django.shortcuts import render
from .models import UserFav, UserLeavingMessage, UserAddress
from .serializers import UserFavSerializer, UserFavDetailSerializer, UserLeavingMessageSerializer, UserAddressSerializer
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.authentication import SessionAuthentication
from .permission import IsOwnerOrReadOnly
from rest_framework import permissions
from rest_framework import mixins, viewsets
# Create your views here.


class UserFavViewSet(mixins.CreateModelMixin, mixins.DestroyModelMixin,
                     mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet):
    """
    create:
    用户收藏
    list:
    用户收藏列表
    retrieve:
    收藏商品详情
    destroy:
    取消收藏
    """
    queryset = UserFav.objects.all()
    lookup_field = 'goods_id'
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)
    permission_classes = (permissions.IsAuthenticated, IsOwnerOrReadOnly, )

    def get_queryset(self):
        queryset = UserFav.objects.filter(user=self.request.user)
        return queryset

    def get_serializer_class(self):
        if self.action == 'list':
            return UserFavDetailSerializer
        else:
            return UserFavSerializer


class UserLeavingMessageViewSet(mixins.ListModelMixin, mixins.DestroyModelMixin, mixins.CreateModelMixin,
                                viewsets.GenericViewSet):
    """
    list:
        获取留言列表
    create:
        添加留言
    delete:
        删除留言功能
    """
    queryset = UserLeavingMessage.objects.all()
    serializer_class = UserLeavingMessageSerializer
    permission_classes = (IsOwnerOrReadOnly, permissions.IsAuthenticated)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)

    def get_queryset(self):
        return UserLeavingMessage.objects.filter(user=self.request.user)


class AddressViewSet(viewsets.ModelViewSet):
    """
    list:
    地址列表
    create:
    新增地址
    retrieve:
    地址详情信息
    update:
    修改地址
    """

    serializer_class = UserAddressSerializer
    queryset = UserAddress.objects.all()
    permission_classes = (IsOwnerOrReadOnly, permissions.IsAuthenticated)
    authentication_classes = (JSONWebTokenAuthentication, SessionAuthentication)

    def get_queryset(self):
        return UserAddress.objects.filter(user=self.request.user)
